package com.itheima;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@WebServlet("/addstudentservlet")
public class addstudentservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取学生信息
        String username = req.getParameter("username");
        String age = req.getParameter("age");
        String score = req.getParameter("score");
        //存入学生对象
        Student student = new Student(username, Integer.parseInt(age), Integer.parseInt(score));
        //存入本地文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\e.txt",true));
        bw.write(student.getUsername()+","+student.getAge()+","+student.getScore());
        bw.newLine();
        bw.close();
        //响应消息2秒钟后返回首页
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("添加成功,2秒钟后返回首页");
        resp.setHeader("Refresh","2;URL=/stu/index.jsp");



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
